﻿// Behavior created by Liferose
// Note: It's an early revision which needs more 
// interaction handling.

#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;

using Bots.Grind;
using Buddy.Coroutines;
using CommonBehaviors.Actions;
using Honorbuddy.QuestBehaviorCore;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.POI;
using Styx.CommonBot.Frames;
using Styx.CommonBot.Profiles;
using Styx.CommonBot.Routines;
using Styx.Helpers;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.World;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;
#endregion


namespace Honorbuddy.Quest_Behaviors.LifeProject.SpecificQuests.Kalimdor.Ashenvale
{
    [CustomBehaviorFileName(@"Vortex")]
    public class Vortex : QuestBehaviorBase
    {
        #region Constructor and Argument Processing
        public Vortex(Dictionary<string, string> args)
            : base(args)
        {
            QBCLog.BehaviorLoggingContext = this;
            
            try
            {
                QuestId = GetAttributeAsNullable<int>("QuestId", false, ConstrainAs.QuestId(this), null) ?? 0;
                QuestRequirementComplete = QuestCompleteRequirement.NotComplete;
                QuestRequirementInLog = QuestInLogRequirement.InLog;
            }

            catch (Exception except)
            {
                // Maintenance problems occur for a number of reasons.  The primary two are...
                // * Changes were made to the behavior, and boundary conditions weren't properly tested.
                // * The Honorbuddy core was changed, and the behavior wasn't adjusted for the new changes.
                // In any case, we pinpoint the source of the problem area here, and hopefully it can be quickly
                // resolved.
                QBCLog.Exception(except);
                IsAttributeProblem = true;
            }
        }

        protected override void EvaluateUsage_DeprecatedAttributes(XElement xElement)
        {
        }

        protected override void EvaluateUsage_SemanticCoherency(XElement xElement)
        {
        }

        public int QuestId { get; private set; }
        public QuestCompleteRequirement QuestRequirementComplete { get; private set; }
        public QuestInLogRequirement QuestRequirementInLog { get; private set; }
        #endregion


        #region Private and Convenience variables
        public int VortexGiverId = 34289;
        public int WhirlingVortexId = 34322;
        public int MagmatharId = 34295;
        public int SkyLightningId = 65231;

        public LocalPlayer Me { get { return (StyxWoW.Me); } }
        #endregion

        public WoWPetSpell AttackSpell 
        { 
            get 
            { 
                return Me.PetSpells.FirstOrDefault(s => s.Spell != null && s.Spell.Id == SkyLightningId); 
            } 
        }

        public WoWUnit VortexGiver
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>()
                    .Where(u => u.Entry == VortexGiverId && !u.IsDead && u.Distance < 100)
                    .OrderBy(u => u.Distance).FirstOrDefault();
            }
        }

        public WoWUnit WhirlingVortex
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>()
                    .Where(u => u.Entry == WhirlingVortexId && !u.IsDead && u.Distance < 10)
                    .OrderBy(u => u.Distance).FirstOrDefault();
            }
        }

        public WoWUnit Magmathar
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>()
                    .Where(u => u.Entry == MagmatharId && !u.IsDead && u.Distance < 150)
                    .OrderBy(u => u.Distance).FirstOrDefault();
            }
        }

        public override void OnStart()
        {
            var isBehaviorShouldRun = OnStart_QuestBehaviorCore(string.Empty);

            if (isBehaviorShouldRun)
            {
                LevelBot.BehaviorFlags &=
                    ~(BehaviorFlags.Combat | BehaviorFlags.Vendor);
            }
        }

        protected override Composite CreateBehavior_CombatOnly()
        {
            return new PrioritySelector(
                new Decorator(context => Query.IsInVehicle(),
                    new ActionAlwaysSucceed())
                );
        }

        protected override Composite CreateMainBehavior()
        {
            return new PrioritySelector(
                new Decorator(context => !Query.IsInVehicle() && VortexGiver != null,
                        new Sequence(
                            // Add Move to VortexGiver xyz if null
                            new Action(context => { VortexGiver.Interact(); }),
                            new Action(context => { GossipFrame.Instance.SelectGossipOption(0); })      
                            )
                        ),
                new Decorator(context => Query.IsInVehicle() && Magmathar != null && !SpellManager.GlobalCooldown,
                    new PrioritySelector(
                        new Decorator(context => WhirlingVortex.HasAura("Lordly Immolate"),
                            new Sequence(
                                new Action(context => { Lua.DoString("CastPetAction(3)"); }),
                                new WaitContinue(TimeSpan.FromMilliseconds(1250), context => false, new ActionAlwaysSucceed())
                                )
                            ),
                        new Decorator(context => !Magmathar.HasAura("Vortex Vengeance"),
                            new Sequence(
                                new Action(context => { Magmathar.Target(); }),
                                new Action(context => { Lua.DoString("CastPetAction(2)"); }),
                                new WaitContinue(TimeSpan.FromMilliseconds(1250), context => false, new ActionAlwaysSucceed())
                                )
                            ),
                        new Decorator(context => !AttackSpell.Spell.Cooldown,
                            new Sequence(
                                new Action(context => { Magmathar.Target(); }),
                                new Action(context => { Lua.DoString("CastPetAction(1)"); }),
                                new WaitContinue(TimeSpan.FromMilliseconds(1250), context => false, new ActionAlwaysSucceed())
                                )
                            ))
                    ));
        }
    }
}
